using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//商品分类
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class FontCommSpeController : ControllerBase
    {
        private IRepository<CommodityDetails> _commskuRepository;
        private IRepository<Commodities> _commdityRepository;
        private IRepository<CommodityDetailPicture> _commpictureRepository;
        private IRepository<Specifications> _speRepository;

        public FontCommSpeController(
            IRepository<CommodityDetails> commskuRepository,
            IRepository<CommodityDetailPicture> commpictureRepository,
            IRepository<Specifications> speRepository,
            IRepository<Commodities> commdityRepository
        )
        {
            _commskuRepository = commskuRepository;
            _commdityRepository = commdityRepository;
            _commpictureRepository = commpictureRepository;
            _speRepository = speRepository;

        }


        //通过商品Id获取商品Spe
        [HttpGet("{id}")]
        public dynamic GetAllSpebyId(int id)
        {
            var skubyuserId = _commskuRepository.Table.Where(x => x.CommodityId == id && x.IsDelete == false).ToList();


            // skubyuserId.ForEach(item =>
            // {
            //     // item.SpecificationId="1,2"[1,2]
            //     string[] a = new string[] { };
            //     List<string> b = a.ToList();
            //     string[] strArray = item.SpecificationId.Split(',');

            // });

            // var spu = from commsku in _commskuRepository.Table
            //           join commspu in _commdityRepository.Table on commsku.CommodityId equals commspu.Id
            //           where commsku.IsDelete == false && commsku.IsGrounding==true
            //           select new { Id = commsku.Id, CommodityId=commspu.Id,CommodityName=commspu.CommodityName,
            //             CommodityExplain=commspu.CommodityExplain,Price=commsku.Price,Inventory=commsku.Inventory,
            //             CreateTime=commsku.CreateTime,IsEnable=commsku.IsGrounding
            //           };




            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = skubyuserId,
                Msg = "查询成功"
            });
        }
        //通过商品传入的子分类ID串查找
        [HttpPost,Route("newskubyid")]
        public dynamic GetskubynewId(NewStrspec model)
        {
            var str=model.SpeChildId;
            var sku=_commskuRepository.Table.Where(x=>x.SpeChildId==str && x.IsDelete==false).FirstOrDefault();
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = sku,
                Msg = "查询成功"
            });
        }


    }
}
